package com.zwps.common.tool.shell;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ShellProcessTool {

    static class ProcessHandleRunnable implements Runnable {
        private Process process;

        private String print;

        private List<String> printLines;

        public ProcessHandleRunnable(Process process) {
            this.process = process;
        }

        public void run() {
            read(process.getInputStream());
        }

        private void read(InputStream input) {
            try (InputStream is = input) {
                try (InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr)) {
                    StringBuilder build = new StringBuilder();
                    String line;
                    printLines = new ArrayList<String>();
                    while ((line = br.readLine()) != null) {
                        build.append(line);
                        printLines.add(line);
                    }
                    print = build.toString();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 其中一种执行shell命令方法，因为某些时候不能正常获取打印内容就使用executeCmd方法
     * 不建议使用这个，这个在处理参数包含空格时会按空格1截断
     * @Title: executeCmd2
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @param command
     * @return
     * @throws Exception List<String>
     * @date 2023年3月7日
     */
    public static List<String> executeCmd2(String... command) throws Exception {
        System.out.println("command: " + command);
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(command);
        ProcessHandleRunnable processHandle = new ProcessHandleRunnable(process);
        new Thread(processHandle).start();
        if (process.waitFor() != 0) {
            processHandle.read(process.getErrorStream());
            System.out.println("executeCmd2: " + command);
            throw new Exception(processHandle.print);
        }
        return processHandle.printLines;
    }

    public static List<String> executeCmd(List<String> command) throws Exception {
//        String[] commandSplit = command.split(" ");
//        List<String> lcommand = new ArrayList<>();
//        for (int i = 0; i < commandSplit.length; i++) {
//            if (commandSplit[i] != null) {
//                lcommand.add(commandSplit[i]);
//                System.out.println(commandSplit[i]);
//            }
//        }
        ProcessBuilder processBuilder = new ProcessBuilder(command);
        processBuilder.redirectErrorStream(true);
        Process process = processBuilder.start();
        ProcessHandleRunnable processHandle = new ProcessHandleRunnable(process);
        new Thread(processHandle).start();
        if (process.waitFor() != 0) {
            processHandle.read(process.getErrorStream());
            System.out.print("executeCmd:");
            for (String string : command) {
                System.out.print(" "+string);
            }
            System.out.println();
            throw new Exception(processHandle.print);
        }
        return processHandle.printLines;
    }

}